package com.zktr.dao;

import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.zktr.pojo.OrderInfo;
import com.zktr.pojo.Orders;
import com.zktr.pojo.Users;
import com.zktr.utils.CRUDDAO;

public class UserDao {
	CRUDDAO<Users> crud=new CRUDDAO<>(Users.class);
	/**
	 * 测试通过：
	 */
	public static void main(String[] args) {
		/*OrdersDao dao=new OrdersDao();
		int sum = dao.selectOrdersCount();
		Map<Integer, List<Orders>> selectOrders = dao.selectOrders(4,3);
		List<Orders> list = selectOrders.get(0);
		for (Orders orders : list) {
			System.out.println(orders);
		}
		*/
    UserDao userDao = new UserDao();
    Users users = new Users();
    users.setUsername("梁玉洁");
    System.out.println(userDao.selectName(users));
	}
	/**
	 * 分页查询总记录条数：
	 * @return 
	 * 
	 */
	public int  selectOrderinfoCount() {
		return crud.selectCount("select count(*) FROM users");
	}
	/**
	 * 分页查询所有会员：
	 *  pc:当前页
	 * ps:页大小
	 * ,user_rank WHERE users.userrank=user_rank.rankrank
	 * @return 
	 */
	public Map<Integer, List<Users>> selectUsers(int pc, int ps) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("select * FROM users LIMIT ?,?",new Object[]{(pc-1)*ps,ps});
		return 	crud.selectAll(map);
	}
	/**
	 * 查询单个：
	 * @param users
	 * @return
	 */
	public List<Users> selectOneUser(Users users) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("select * from users where userid=?", new Object[] {users.getUserid()});
		return crud.selectAll(map).get(0);
	}
	/**
	 * 修改的方法：
	 * @param users
	 * @return
	 */
	public int updateuser(Users users) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("update users set username=?,mobilephone=?,rankpoints=?,userrank=? where userid=?", 
				new Object[]{users.getUsername(),users.getMobilephone(),users.getRankpoints(),users.getUserrank(),users.getUserid()});
		 return crud.updateAll(map)[0];
	}
	/**
	 * 查询名字：
	 * @param users
	 * @return
	 */
	public List<Users> selectName(Users users) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("select * from users where username=?", new Object[] {users.getUsername()});
		return crud.selectAll(map).get(0);	
		}
	/**
	 * 新增会员：
	 * @param users
	 * @return
	 */
	public int addUsers(Users users) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("insert into users(userid,username,email,password,sex,birthday,paypoints,rankpoints,regtime,mobilephone)values(?,?,?,?,?,?,?,?,?,?)", 
				new Object[]{users.getUserid(),users.getUsername(),users.getEmail(),users.getPassword(),users.getSex(),users.getBirthday(),users.getPaypoints(),users.getRankpoints(),users.getRegtime(),users.getMobilephone()});
		 return crud.updateAll(map)[0];
	}
	/**
	 * 会员排行：
	 * @return
	 */
	public Map<Integer, List<Users>> selectUsers() {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("select * from users ORDER BY users.rankpoints DESC LIMIT 0,10",null);
		return 	crud.selectAll(map);
	}
	/**
	 * 删除会员：
	 * @param userid
	 * @return
	 */
	public int deleteorders(int userid) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("delete from users WHERE userid=?", 
				new Object[]{userid});
		map.put("delete from user_address WHERE orderid=?", new Object[] {userid});
		 return crud.updateAll(map)[0];
	}
}
